HTMLify
349. Intersection of Two Arrays.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // 349. Intersection of Two Arrays class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set = new HashSet<>(); Set<Integer> intersectionSet = new HashSet<>(); for (int num : nums1) { set.add(num); } for (int num : nums2) { if (set.contains(num)) { intersectionSet.add(num); } } int[] result = new int[intersectionSet.size()]; int i = 0; for (int num : intersectionSet) { result[i++] = num; } return result; } } |